<template>
  <div class="app-container">
    <div class="filter-container">

      <el-input v-model="listQuery.params.deptName" style="width: 200px" placeholder="搜索部门名称" class="filter-item" />

      <el-button v-permission="['sys:depart:add']" class="filter-item" type="primary" icon="el-icon-plus" @click="formDialog(0)">
        添加
      </el-button>

    </div>

    <el-table
      ref="table"
      :data="tableData.records"
      row-key="id"
      border
      :default-expand-all.sync="defaultExpand"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
      :header-cell-style="{'background':'#f2f3f4', 'color':'#555', 'font-weight':'bold', 'line-height':'32px'}"
    >
      <el-table-column label="名称" prop="deptName" />
      <el-table-column label="编码" prop="deptCode" />

      <el-table-column align="center" label="排序">
        <template slot-scope="scope">
          <el-tooltip v-permission="['sys:depart:update']" class="item" effect="dark" content="向下移动" placement="bottom">
            <el-button size="small" icon="el-icon-sort-down" circle @click="handleSort(scope.row.id, 1)" />
          </el-tooltip>

          <el-tooltip v-permission="['sys:depart:update']" class="item" effect="dark" content="向上移动" placement="bottom">
            <el-button size="small" icon="el-icon-sort-up" circle @click="handleSort(scope.row.id, 0)" />
          </el-tooltip>

        </template>
      </el-table-column>

      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-tooltip v-permission="['sys:depart:add']" class="item" effect="dark" content="添加下级部门" placement="bottom">
            <el-button size="small" icon="el-icon-plus" circle @click="formDialog(scope.row.id)" />
          </el-tooltip>

          <el-tooltip v-permission="['sys:depart:update']" class="item" effect="dark" content="修改" placement="bottom">
            <el-button size="small" icon="el-icon-edit" circle @click="formDialog(scope.row.parentId, scope.row.id)" />
          </el-tooltip>

          <el-tooltip v-permission="['sys:depart:delete']" class="item" effect="dark" content="删除" placement="bottom">
            <el-button size="small" icon="el-icon-delete" circle @click="handleDelete(scope.row.id)" />
          </el-tooltip>

        </template>

      </el-table-column>


      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-link
            v-permission="['sys:user']"
            style="margin-left: 10px"
            type="primary"
            icon="el-icon-user"
            @click="showUser(scope.row.deptCode)"
          >用户列表</el-link>

        </template>

      </el-table-column>




    </el-table>

    <pagination v-show="tableData.total>0" :total="tableData.total" :page.sync="listQuery.current" :limit.sync="listQuery.size" @pagination="getList" />

    <el-dialog
      title="维护部门"
      width="30%"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :visible.sync="dialogFormVisible"
    >
      <el-form ref="postForm" :model="postForm" :rules="rules" label-width="100px" label-position="left">
        <el-form-item label="部门名称" prop="deptName">
          <el-input v-model="postForm.deptName" />
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleSave">确 定</el-button>
      </div>
    </el-dialog>


    <el-drawer
      size="70%"
      title="用户列表"
      :wrapper-closable="false"
      :visible.sync="userVisible"
      direction="rtl"
    >
      <sub-user-list :dept-code="deptCode" />
    </el-drawer>

  </div>
</template>

<script>
import { pagingTree, fetchDetail, deleteData, saveData, sortData } from '@/api/sys/depart/depart'
import Pagination from '@/components/Pagination'
import permission from '@/directive/permission'
import SubUserList from '@/views/admin/sys/user/components/SubUserList'

export default {
  name: 'Depart',
  components: { SubUserList, Pagination },
  directives: { permission },
  data() {
    return {
      defaultExpand: true,
      postForm: {},
      rules: {
        deptName: [
          { required: true, message: '部门名称不能为空！' }
        ]
      },
      dialogFormVisible: false,
      tableData: {
        total: 0,
        records: []
      },
      listLoading: true,
      listQuery: {
        current: 1,
        size: 10,
        params: {

        }
      },
      userVisible: false,
      deptCode: ''
    }
  },
  watch: {

    // 检测查询变化
    listQuery: {
      handler() {
        this.getList()
      },
      deep: true
    }
  },
  created() {
    this.getList()
  },
  methods: {


    // 查看用户列表
    showUser(deptCode) {
      this.deptCode = deptCode
      this.userVisible = true
    },

    /**
       * 编辑窗口
       * @param parentId
       * @param currentId
       */
    formDialog(parentId, currentId) {
      // 清空表单
      this.postForm = {}

      // 覆盖上级分类
      this.postForm.refType = this.listQuery.refType
      this.postForm.parentId = parentId

      // 如果是修改
      if (currentId != null) {
        fetchDetail(currentId).then(response => {
          this.postForm = response.data
        })
      }

      this.dialogFormVisible = true
    },

    getList() {
      this.listLoading = true
      pagingTree(this.listQuery).then(response => {
        this.tableData = response.data
        this.listLoading = false
      })
    },

    handleSort(id, sort) {
      sortData(id, sort).then(() => {
        this.$notify({
          title: '成功',
          message: '部门排序成功！',
          type: 'success',
          duration: 2000
        })

        this.getList()
      })
    },

    handleSave() {
      this.$refs.postForm.validate((valid) => {
        if (!valid) {
          return
        }

        saveData(this.postForm).then(() => {
          this.$notify({
            title: '成功',
            message: '部门保存成功！',
            type: 'success',
            duration: 2000
          })

          this.dialogFormVisible = false
          this.getList()
        })
      })
    },

    handleDelete(id) {
      // 删除
      this.$confirm('确实要删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const ids = []
        ids.push(id)

        deleteData(ids).then(() => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })

          this.getList()
        }).catch(err => {
          console.log(err)
        })
      })
    }
  }
}
</script>
